package com.xiangban.volunteer_platform.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiangban.volunteer_platform.dao.OrderMapper;
import com.xiangban.volunteer_platform.entity.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author Mr. Ma
 * @Data 2021/1/22 15:01
 */
@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;


    public void saveOrder(Orders orders){
        orderMapper.insert(orders);

    }

    public int delOrder(Orders orders) {
        Orders orders1 = orderMapper.selectById(orders.getId());
        if (orders1 !=null){
           return orderMapper.deleteById(orders.getId());
        }
        return -1;
    }

    public int updateOrder(Orders orders){
        Orders orders1 = orderMapper.selectById(orders.getId());
        if (orders1 !=null){
               return orderMapper.updateById(orders);
        }
       return -1;
    }

    public IPage<Orders> getAllOrder(Integer pageNum, Integer pageSize, Orders orders){
        IPage<Orders> page = new Page<>(pageNum == null ? 1:pageNum,
                                        pageSize == null ? 10:pageSize);
        LambdaQueryWrapper<Orders>  queryWrapper = new LambdaQueryWrapper<>();

            if (orders.getId() !=null && !orders.getId().equals("")){
                queryWrapper.eq(Orders::getId, orders.getId());
            }
            if (orders.getUserId() != null && !orders.getUserId().equals("")){
                queryWrapper.eq(Orders::getUserId, orders.getUserId());
            }


        return orderMapper.selectPage(page,queryWrapper);
    }
}
